package runtime

import (
	"fmt"
	cfg "my_gin_open/build"
	"my_go_open/utils"
	"strings"
	"time"
)

type RTLog struct {
	When  int64
	Which string
	What  []interface{}
}

var runtimeLogQueue *utils.Queue

func init() {
	runtimeLogQueue, _ = utils.NewQueue(5000)
	go func() {
		for {
			runtimeLogQueue.PopAllWalk(func(item interface{}) {
				if rtLog, ok := item.(*RTLog); ok {
					strB := new(strings.Builder)
					defer strB.Reset()
					strB.WriteString(fmt.Sprintf("-- %s\n", utils.TimeFormStamp(rtLog.When)))
					for i, d := range rtLog.What {
						strB.WriteString(fmt.Sprintf("%d] %+v\n", i, d))
					}
					f := fmt.Sprintf(
						"%s/%s/%s/%d.txt",
						cfg.RuntimeLogDir,
						rtLog.Which,
						utils.TimeDateFromStamp(rtLog.When),
						time.Unix(rtLog.When, 0).In(time.FixedZone("CST", 28800)).Hour(),
					)
					go func() {
						_, _ = utils.FileAppend(f, strB.String())
					}()
				}
			})
			time.Sleep(time.Second)
		}
	}()
}
